//
//  STDSAttrib.cpp
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/13.
//

#include "STDSAttrib.hpp"


void STDSAttrib::draw(){
    if (mCountVertices <= 0)
        return;

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glVertexAttribPointer((GLuint) mPosVex, 3, GL_FLOAT, GL_FALSE, 0, mArrVex);
    glEnableVertexAttribArray((GLuint) mPosVex);

    if(mPosTex >= 0){
        glVertexAttribPointer((GLuint) mPosTex, 2, GL_FLOAT, GL_FALSE, 0, mArrTex);
        glEnableVertexAttribArray((GLuint) mPosTex);
    }

    if(mPosClr >= 0){
        glVertexAttribPointer((GLuint) mPosClr, 4, GL_FLOAT, GL_FALSE, 0, mArrClr);
        glEnableVertexAttribArray((GLuint) mPosClr);
    }

    mDrawFunc(mArrIdx, mCountVertices, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void STDSAttrib::_reset() {
    _resetCount();
    if(mDirtyIdx){
        _resetIdx();
        mDirtyIdx = false;
    }
    if(mDirtyVex){
        _resetVex();
        mDirtyVex = false;
    }
    if(mDirtyTex){
        _resetTex();
        mDirtyTex = false;
    }
    if(mDirtyClr){
        _resetClr();
        mDirtyClr = false;
    }
    _resetDrawFunc();
}

void STDSAttrib::_release() {
    SAFE_DELETE_ARRAY(mArrIdx);
    SAFE_DELETE_ARRAY(mArrVex);
    SAFE_DELETE_ARRAY(mArrParam);
    SAFE_DELETE_ARRAY(mArrTex);
    SAFE_DELETE_ARRAY(mArrClr);
}
